﻿using Minotaur.App.Core.Contrls;
using Minotaur.App.Domains.Dto.Flow;
using Newtonsoft.Json;
using ServiceStack;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;

namespace Minotaur.App.Modules.FlowProcess.Control
{
    /// <summary>
    /// Interaction logic for FlowLineControl1
    /// </summary>
    public partial class FlowLineControl1 : UserControl
    {
        public FlowLineControl1()
        {
            InitializeComponent();
        }
        public FlowLineDto FlowLineDtos;
        public long StartId { get; set; }
        public long EndId { get; set; }
        public Button StartButton { get; set; }
        public Button EndButton { get; set; }
        public FlowLineControl1(FlowLineDto flowLineDto, Button _StartButton, Button _EndButton)
        {
            InitializeComponent();
            FlowLineDtos = flowLineDto;
            double[] startAt = JsonConvert.DeserializeObject<double[]>(flowLineDto.StartAt);
            double[] endAt = JsonConvert.DeserializeObject<double[]>(flowLineDto.EndAt);
            lineMain.Points = new PointCollection() { new Point() { X = startAt[0], Y = startAt[1], }, new Point() { X = endAt[0], Y = endAt[1] } };

            ControlCommon.CalculateArrow(lineTopArrow, new Point() { X = startAt[0], Y = startAt[1], }, new Point() { X = endAt[0], Y = endAt[1] });
            StartId = flowLineDto.StartId.Value;
            EndId = flowLineDto.EndId.Value;
            StartButton = _StartButton;
            EndButton = _EndButton;
        }

        private void thmEnd_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Thumb thm = (Thumb)sender;
            double y = Canvas.GetTop(this) + e.VerticalChange;
            double x = Canvas.GetLeft(this) + e.HorizontalChange;
            Canvas.SetTop(thm, Canvas.GetTop(thm) + e.VerticalChange);
            Canvas.SetLeft(thm, Canvas.GetLeft(thm) + e.HorizontalChange);
        }

        /// <summary>
        /// 打开连线属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpendLineProperty_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(OpendLineEvent, FlowLineDtos);
            RaiseEvent(args);
        }

        private void DeleteLine_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventArgs args = new RoutedEventArgs(DeleteLineEvent, FlowLineDtos);
            RaiseEvent(args);
        }
        /// <summary>
        /// 注册打开连线配置事件
        /// </summary>
        public static readonly RoutedEvent OpendLineEvent =
              EventManager.RegisterRoutedEvent(ClickNameConst.OpendLineClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FlowLineControl1));

        /// <summary>
        /// 注册连线删除事件
        /// </summary>
        public static readonly RoutedEvent DeleteLineEvent =
             EventManager.RegisterRoutedEvent(ClickNameConst.DeleteLineEventClick, RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(FlowLineControl1));

        /// <summary>
        /// 删除连线事件
        /// </summary>
        public event RoutedEventHandler DeleteLineEventClick
        {
            add { AddHandler(DeleteLineEvent, value); }
            remove { RemoveHandler(DeleteLineEvent, value); }
        }

        /// <summary>
        /// 打开连线属性事件
        /// </summary>
        public event RoutedEventHandler OpendLineClick
        {
            add { AddHandler(OpendLineEvent, value); }
            remove { RemoveHandler(OpendLineEvent, value); }
        }
    }
}
